<?php
//including page.php (It is a class which contain functions)
include 'page.php';
//created object for page.php
$page = new page();

if (isset($_POST['save'])) {
    $bill_date=$_POST['bill_date'];
      $bill_to=$_POST['bill_to'];
      echo $bill_to;
    $total_rate=$_POST['total_rate'];
    $total_discount=$_POST['total_discount'];
    $total_vat=$_POST['total_vat'];
    $bill_total=$_POST['bill_total'];
    
    $display = unserialize(base64_decode($_POST['display']));
    $totalamount=$total_rate+$total_discount;
    $insert=mysql_query("INSERT INTO bills ( date, billto, total, discount, amount, vat )
                       VALUES
                       ( '$bill_date', '$bill_to', '$totalamount', '$total_discount', '$total_rate', '$total_vat')") or die(mysql_error());
    if(!$insert) {
        echo "Failed to insert data into bills table at save_sales_billing.php";
    }
   
$result = $display;
$bills = mysql_query("select * from bills where date = '$bill_date'");
    $bills_data = mysql_fetch_array($bills);
   $bills_no=$bills_data['billno'];
$dsc = mysql_query("select * from options");
$dsc_data = mysql_fetch_array($dsc);
for ($row = 0; $row < count($result); $row++) {
    // echo "  Name :" . $result[$row]['productid'] . " <br> qty :" . $result[$row]['qty'];
    // echo "<br />";

    $product_id = $result[$row]['productid'];
    $discount = $result[$row]['discount'];
    $qty=$result[$row]['qty'];

    $products = mysql_query("select * from products where productid = $product_id");
    $products_data = mysql_fetch_array($products);
echo $products_data['price'];
    if ($dsc_data['discount_type'] == "amount") {
        $total = ($products_data['price'] - $discount) * $result[$row]['qty'];
        $dup_total = $total_rate + $total;
        $total_rate = $dup_total;
        $vat_tax = ($total / 100) * 14.5;
        $amount = $total + $vat_tax;
        $dup_vat = $total_vat + $vat_tax;
        $total_vat = $dup_vat;
        $bill_total = $total_rate + $total_vat;
        $dup_discount = $total_discount + ($discount * $result[$row]['qty']);
        $total_discount = $dup_discount;
        
      $insert_details= mysql_query("INSERT INTO billdetails ( billno, productid, qty, rate, total )
                       VALUES
                       ( '$bills_no','$product_id', '$qty, '$total', '$total')")  or die(mysql_error());
       if(!$insert_details) {
       header('Location: ../modules/sales/add_sales_billing?status=0');
    }
    else {
          header('Location: ../modules/sales/add_sales_billing?status=1');
    }
   
    } else {
        $total = ($products_data['price'] - ((($products_data['price']) / 100) * $discount)) * $result[$row]['qty'];
        $dup_total = $total_rate + $total;
        $total_rate = $dup_total;
        $vat_tax = ($total / 100) * 14.5;
        $amount = $total + $vat_tax;
        $dup_vat = $total_vat + $vat_tax;
        $total_vat = $dup_vat;
        $bill_total = $total_rate + $total_vat;
        $dup_discount = $total_discount + (((($products_data['price']) / 100) * $discount) * $result[$row]['qty']);
        $total_discount = $dup_discount;
        
      $insert_details= mysql_query("INSERT INTO billdetails ( billno, productid, qty, rate, total )
                       VALUES
                       ( '$bills_no','$product_id', '$qty', '$total', '$total')") or die(mysql_error());
    if(!$insert_details) {
       header('Location: ../modules/sales/add_sales_billing?status=0');
    }
    else {
          header('Location: ../modules/sales/add_sales_billing?status=1');
    }
    }
      
  
   
 
}
}
?>